package cn.cs.service;

import cn.cs.common.MD5;
import cn.cs.common.RedisUtils;
import cn.cs.common.UserAgentUtil;
import cn.cs.mapper.RepairUserMapper;
import cn.cs.pojo.*;
import com.alibaba.fastjson.JSON;
import cz.mallat.uasparser.UserAgentInfo;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.List;
import java.util.Map;

@RestController
public class RestRepairUserService {

    private String tokenPrefix = "token:";

    @Resource
    private RedisUtils redisUtils;
    @Resource
    private RepairUserMapper mapper;

    @RequestMapping(value = "/createToken", method = RequestMethod.POST)
    @Transactional
    public String createToken(@RequestParam("agent") String agent,@RequestBody RepairUser user) throws Exception {
        try {
        UserAgentInfo userAgentInfo = UserAgentUtil.getUasParser().parse(agent);
        StringBuilder sb = new StringBuilder();
        sb.append(tokenPrefix);//统一前缀
        String agentStr = "";
        if (userAgentInfo.getDeviceType().equals(UserAgentInfo.UNKNOWN)) {
            if (UserAgentUtil.CheckAgent(agent)) {
                sb.append("MOBILE-");
                agentStr = "MOBILE";
            } else {
                sb.append("PC-");
                agentStr = "PC";
            }
        } else if (userAgentInfo.getDeviceType()
                .equals("Personal computer")) {
            sb.append("PC-");
            agentStr = "PC";
        } else{
            agentStr = "MOBILE";
            sb.append("MOBILE-");
        }
//			sb.append(user.getUserCode() + "-");
        sb.append(MD5.getMd5(user.getPhone(),32) + "-");//加密用户名称
        sb.append(user.getId() + "-");
        sb.append(MD5.getMd5(user.getId(), 6)
                + "-");
        sb.append(MD5.getMd5(agentStr, 6));// 识别客户端的简化实现——6位MD5码

        return sb.toString();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return  null;
    }


    @RequestMapping(value = "/saveToken", method = RequestMethod.POST)
    @Transactional
    public void saveToken(@RequestParam("token")String token,@RequestBody RepairUser user) throws Exception {
        if (token.startsWith(tokenPrefix+"PC-"))
            redisUtils.set(token, 2*60*60, JSON.toJSONString(user));
        else
            redisUtils.set(token, JSON.toJSONString(user));// 手机认证信息永不失效
    }

    @RequestMapping(value = "/getCerListByTypes", method = RequestMethod.POST)
    @Transactional
    public List<Map<String, Object>> getCerListByTypes(@RequestBody Map<String, Object> param) {
        return mapper.getCerListByTypes(param);
    }
    @RequestMapping(value = "/insertRepairCertificateImg", method = RequestMethod.POST)
    @Transactional
    public int insertRepairCertificateImg(@RequestBody RepairCertificateImg repImg) {
        return   mapper.insertRepairCertificateImg(repImg);
    }
    @RequestMapping(value = "/checkIdCardNum", method = RequestMethod.POST)
    @Transactional
    public int checkIdCardNum(@RequestParam("idCard") String idCard) {
        return mapper.checkIdCardNum(idCard);
    }
    @RequestMapping(value = "/insertRepairUser", method = RequestMethod.POST)
    @Transactional
    public int insertRepairUser(@RequestBody RepairUser user) {
        return mapper.insertRepairUser(user);
    }


    @RequestMapping(value = "/getRepairCerfImgs", method = RequestMethod.POST)
    @Transactional
    public List<RepairCertificateImg> getRepairCerfImgs(@RequestParam("id") String id){
        return  mapper.getRepairCerfImgs(id);
    }
    @RequestMapping(value = "/deleteRepairAreaById", method = RequestMethod.POST)
    @Transactional
    public int deleteRepairAreaById(@RequestParam("id") String id){
        return  mapper.deleteRepairAreaById(id);
    }
    @RequestMapping(value = "/insertRepAreas", method = RequestMethod.POST)
    @Transactional
    public int insertRepAreas(@RequestBody RepairUserArea repairUserArea){
        return  mapper.insertRepAreas(repairUserArea);
    }
    @RequestMapping(value = "/updateRepairUser", method = RequestMethod.POST)
    @Transactional
    public int updateRepairUser(@RequestBody RepairUser newUser){
        return  mapper.updateRepairUser(newUser);
    }
    @RequestMapping(value = "/getRepairUserByPhone", method = RequestMethod.POST)
    @Transactional
    public RepairUser getRepairUserByPhone(@RequestParam("phone")String phone){
        return  mapper.getRepairUserByPhone(phone);
    }

    @RequestMapping(value = "/checkInvitationCode", method = RequestMethod.POST)
    @Transactional
    public int checkInvitationCode(@RequestParam("invitationCode")String invitationCode){
        return  mapper.checkInvitationCode(invitationCode);
    }
    @RequestMapping(value = "/getMypriLciById", method = RequestMethod.POST)
    @Transactional
    public List<ClientUser> getMypriLciById(@RequestParam("id")String id) {
        return  mapper.getMypriLciById(id);
    }
    @RequestMapping(value = "/getComplaintById", method = RequestMethod.POST)
    @Transactional
    public RepairUserComplaint getComplaintById(@RequestParam("id")String id) {
        return mapper.getComplaintById(id);
    }
    @RequestMapping(value = "/getMyComplaintNodes", method = RequestMethod.POST)
    @Transactional
    public List<RepairUserComplaintNote> getMyComplaintNodes(@RequestParam("id")String id) {
        return mapper.getMyComplaintNodes(id);
    }
    @RequestMapping(value = "/getMyComplaint", method = RequestMethod.POST)
    @Transactional
    public List<RepairUserComplaint> getMyComplaint(@RequestParam("id")String id) {
        return mapper.getMyComplaint(id);
    }
    @RequestMapping(value = "/getCerfImgPath", method = RequestMethod.POST)
    @Transactional
    public String getCerfImgPath(@RequestBody RepairCertificateImg repImg) {
        return mapper.getCerfImgPath(repImg);
    }
    @RequestMapping(value = "/updateCertificateImgs", method = RequestMethod.POST)
    @Transactional
    public int updateCertificateImgs(@RequestBody RepairCertificateImg repImg) {
        return mapper.updateCertificateImgs(repImg);
    }
    @RequestMapping(value = "/getPolicy", method = RequestMethod.POST)
    @Transactional
    List<Map<String,Object>> getPolicy(){
        return  mapper.getPolicy();
    }




    @RequestMapping(value = "/getLastSettlement", method = RequestMethod.POST)
    @Transactional
    public RepSettlement getLastSettlement(String id) {
        return mapper.getLastSettlement(id);
    }
    @RequestMapping(value = "/insertSettOrder", method = RequestMethod.POST)
    @Transactional
    public int insertSettOrder(SettlementOrder settlementOrder) {
        return mapper.insertSettOrder(settlementOrder);
    }
    @RequestMapping(value = "/insertSett", method = RequestMethod.POST)
    @Transactional
    public int insertSett(RepSettlement repSettlement) {
        return mapper.insertSett(repSettlement);
    }



    @RequestMapping(value = "/getRepairUserArea", method = RequestMethod.POST)
    @Transactional
    List<Map<String,Object>> getRepairUserArea(@RequestParam("id") String id){
        return  mapper.getRepairUserArea(id);
    }




    @RequestMapping(value = "/getRepairUserByUserId", method = RequestMethod.POST)
    public Map<String,Object> getRepairUserByUserId(@RequestParam(value = "userId") String userId)throws Exception {
        return mapper.getRepairUserByUserId(userId);
    }

    @RequestMapping(value = "/getServiceTypeList", method = RequestMethod.POST)
    public List<ServiceType> getServiceTypeList(){
        return mapper.getServiceTypeList();
    }

    @RequestMapping(value = "/getFaultTypeByParentId", method = RequestMethod.POST)
    public List<FaultType> getFaultTypeByParentId(@RequestBody() Map<String, Object> param){
        return mapper.getFaultTypeByParentId(param);
    }






    @RequestMapping(value = "/insertCertificateList", method = RequestMethod.POST)
    public int insertCertificateList(@RequestBody Map<String, Object> param){
        return mapper.insertCertificateList(param);
    }

    @RequestMapping(value = "/delCertificateImg", method = RequestMethod.POST)
    public int delCertificateImg(@RequestParam("id") String id){
        return mapper.delCertificateImg(id);
    }

    @RequestMapping(value = "/getRepairUserCountByMap", method = RequestMethod.POST)
    public Integer getRepairUserCountByMap(@RequestBody Map<String, Object> map){
        return mapper.getRepairUserCountByMap(map);
    }

    @RequestMapping(value = "/getRepairUserListByMap", method = RequestMethod.POST)
    public List<RepairUser> getRepairUserListByMap(@RequestBody Map<String, Object> map){
        return mapper.getRepairUserListByMap(map);
    }

    @RequestMapping(value = "/getRepairUserDayCountByMap", method = RequestMethod.POST)
    public Integer getRepairUserDayCountByMap(@RequestBody Map<String, Object> map){
        return mapper.getRepairUserDayCountByMap(map);
    }

    @RequestMapping(value = "/getRepairUserDayByMap", method = RequestMethod.POST)
    public List<Map<String, Object>> getRepairUserDayByMap(@RequestBody Map<String, Object> map){
        return mapper.getRepairUserDayByMap(map);
    }

    @RequestMapping(value = "/getUserCountByPhone", method = RequestMethod.POST)
    public Integer getUserCountByPhone(@RequestParam("phone") String phone){
        return mapper.getUserCountByPhone(phone);
    }

    @RequestMapping(value = "/getCertificateImgByUserId", method = RequestMethod.POST)
    public List<Map<String, Object>> getCertificateImgByUserId(@RequestParam("rep_user_id") String rep_user_id){
        return mapper.getCertificateImgByUserId(rep_user_id);
    }

    @RequestMapping(value = "/getCertificateListByFaultId", method = RequestMethod.POST)
    public List<Map<String, Object>> getCertificateListByFaultId(@RequestParam("fault_id") String fault_id){
        return mapper.getCertificateListByFaultId(fault_id);
    }

    @RequestMapping(value = "/updateUserOrderNumber", method = RequestMethod.POST)
    public Integer updateUserOrderNumber(@RequestParam("id") String id)throws Exception{
        return mapper.updateUserOrderNumber(id);
    }

    @RequestMapping(value = "/getRepairUserById", method = RequestMethod.POST)
    public RepairUser getRepairUserById(@RequestParam("id") String id)throws Exception{
        return mapper.getRepairUserById(id);
    }

    @RequestMapping(value = "/getRepairUserAddressByAreaId", method = RequestMethod.POST)
    public Map<String,Object> getRepairUserAddressByAreaId(@RequestParam("repairUserId") String repairUserId, @RequestParam("area") String area)throws Exception{
        return mapper.getRepairUserAddressByAreaId(repairUserId, area);
    }

    @RequestMapping(value = "/getRepairUserByAreaId", method = RequestMethod.POST)
    public List<Map<String,Object>> getRepairUserByAreaId(@RequestParam("area") String area)throws Exception{
        return mapper.getRepairUserByAreaId(area);
    }

    @RequestMapping(value = "/getRepairUserByLikePhone", method = RequestMethod.POST)
    public List<Map<String, Object>> getRepairUserByLikePhone(@RequestParam("phone") String phone, @RequestParam("personName") String personName)throws Exception{
        return mapper.getRepairUserByLikePhone(phone, personName);
    }

    @RequestMapping(value = "/getUserNumberByAgentCode", method = RequestMethod.POST)
    public Long getUserNumberByAgentCode(@RequestParam("invitationCode") String invitationCode) throws Exception{
        return mapper.getUserNumberByAgentCode(invitationCode);
    }

    @RequestMapping(value = "/getRepairAddressById", method = RequestMethod.POST)
    public Map<String, String> getRepairAddressById(@RequestParam("rep_user_id") String rep_user_id, @RequestParam("address") String address)throws Exception{
        return mapper.getRepairAddressById(rep_user_id, address);
    }


}
